{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 02, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font> Basics of Python: Loops </h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1 </h3>\n",
    "\n",
    "Calculate the value of summation $ 3+6+9+\\cdots+51 $, and the print the result.\n",
    "\n",
    "You should see 459 as the outcome."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "total1 = 0\n",
    "total2 = 0\n",
    "\n",
    "for i in range(3,52,3):\n",
    "    total1 = total1 + i\n",
    "    total2 += i # shorter form\n",
    "\n",
    "print(\"The summation is\",total1)\n",
    "print(\"the summation is\",total2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2 </h3>\n",
    "\n",
    "$ 3^k $ means $ 3 \\cdot 3 \\cdot \\cdots \\cdot 3 $ ($ k $ times) for $ k \\geq 2 $. \n",
    "\n",
    "Moreover, $ 3^0  $ is 1 and $ 3^1 = 3 $.\n",
    "\n",
    "Calculate the value of summation $ 3^0 + 3^1 + 3^2 + \\cdots + 3^8 $, and then print the result.\n",
    "\n",
    "You should see 9841 as the outcome."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T = 0 \n",
    "current_number = 1\n",
    "for i in range(9):\n",
    "    T = T + current_number\n",
    "    print(\"3 to\",i,\"is\",current_number)\n",
    "    current_number = 3 * current_number\n",
    "    \n",
    "print(\"summation is\",T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# python has also exponent operator: **\n",
    "# we may directly use it\n",
    "\n",
    "T = 0\n",
    "\n",
    "for i in range(9):\n",
    "    print(\"3 to\",i,\"is\",3**i)\n",
    "    T = T + 3 ** i\n",
    "    \n",
    "print(\"summation is\",T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task3\"></a>\n",
    "<h3> Task 3 </h3>\n",
    "\n",
    "Consider the summation $ T(n) = 1 + \\dfrac{1}{2} + \\dfrac{1}{4}+ \\dfrac{1}{8} + \\cdots + \\dfrac{1}{2^n} $ for some natural number $ n $.\n",
    "\n",
    "Remark that $ T(0) = \\dfrac{1}{2^0} = \\dfrac{1}{1} = 1 $.\n",
    "\n",
    "This summation can be arbitrarily close to $2$. \n",
    "\n",
    "Let's find the minimum value of $ n $ such that the closeness of $ T(n) $ to $2$ is less than $ 0.01 $.\n",
    "\n",
    "In other words, let's find the minimum value of $n$ such that $ T(n) > 1.99 $.\n",
    "\n",
    "The operator for \"less than or equal to\" in python is \"$ < = $\"."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T = 0\n",
    "n = 2  # this value will be first halved and then added to the summation\n",
    "how_many_terms = 0\n",
    "\n",
    "while T<=1.99:\n",
    "    n = n/2 # half the value of n\n",
    "    T = T + n # update the value of T\n",
    "    how_many_terms = how_many_terms + 1\n",
    "    \n",
    "print(n,T)\n",
    "print(\"how many terms in the summation is\",how_many_terms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# our result says that there should be 8 terms in our summation\n",
    "# let's calculate the summation of the first seven and the first eight terms, and verify our results\n",
    "\n",
    "T7 = 0 \n",
    "n = 2 # this value will be first halved and then added to the summation\n",
    "\n",
    "for i in range(7):\n",
    "    n = n/2\n",
    "    print(n)\n",
    "    T7 = T7 + n\n",
    "    \n",
    "print(\"the summation of the first seven terms is\",T7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T8 = 0 \n",
    "n = 2 # this value will be first halved and then added to the summation\n",
    "\n",
    "for i in range(8):\n",
    "    n = n/2\n",
    "    print(n)\n",
    "    T8 = T8 + n\n",
    "    \n",
    "print(\"the summation of the first eight terms is\",T8)\n",
    "\n",
    "print(\"(the summation of the first seven terms is\",T7,\")\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task4\"></a>\n",
    "<h3> Task 4 </h3>\n",
    "\n",
    "The task is to pick numbers between 0 and 9 randomly until hitting the number 3, and then print the number of attempt(s).\n",
    "\n",
    "We can use <i>randrange</i> function from <i>random</i> module for randomly picking a number in a given range."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "r = 0\n",
    "attempt = 0\n",
    "\n",
    "while r != 3: # the loop iterates as long as r is not equal to 3\n",
    "    r = randrange(10) # randomly pick a number\n",
    "    attempt = attempt + 1 # increase the number of attempts by 1\n",
    "    print (attempt,\"->\",r) # print the number of attmept and the randomly picked number at this moment\n",
    "    \n",
    "print(\"total number of attempt(s) is\",attempt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task5\"></a>\n",
    "<h3> Task 5 </h3>\n",
    "\n",
    "This task is challanging. \n",
    "\n",
    "It is designed for the usage of double nested loops: one loop is inside of the other loop.\n",
    "\n",
    "In the fourth task above, the expected number of attempt(s) to hit number 3 is 10. \n",
    "\n",
    "Let's do a series of experiment by using your solution for Task 4.\n",
    "\n",
    "Experiment 1: Execute your code 20 times, and then calculate the average attempts.\n",
    "\n",
    "Experiment 2: Execute your code 200 times, and then calculate the average attempts.\n",
    "\n",
    "Experiment 3: Execute your code 2000 times, and then calculate the average attempts.\n",
    "\n",
    "Experiment 4: Execute your code 20000 times, and then calculate the average attempts.\n",
    "\n",
    "Experiment 5: Execute your code 200000 times, and then calculate the average attempts.\n",
    "\n",
    "<i>Your experimental average should get closer to 10 when the number of executions is increased.</i>\n",
    "\n",
    "Remark that all five experiments can be automatically done by using triple loops."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# be aware of single and double indentions\n",
    "\n",
    "number_of_execution = 2000 # change this with 200, 2000, 20000, 200000 and reexecute this cell\n",
    "total_attempts = 0  \n",
    "\n",
    "from random import randrange\n",
    "\n",
    "for i in range(number_of_execution): # the outer loops iterates number_of_execution times\n",
    "    r = 0 \n",
    "    attempt = 0\n",
    "    while r != 3: # the while-loop iterates as long as r is not equal to 3\n",
    "        r = randrange(10) # randomly pick a number\n",
    "        attempt = attempt + 1 # increase the number of attempts by 1\n",
    "    # I am out of scope of while-loop\n",
    "    total_attempts = total_attempts + attempt # update the total number of attempts\n",
    "    \n",
    "# I am out of scope of for-loop\n",
    "print(number_of_execution,\"->\",total_attempts/number_of_execution)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's use triple nested loops\n",
    "for number_of_execution in [20,200,2000,20000,200000]: # we will use the same code by indenting all lines one more level :-)\n",
    "    total_attempts = 0 \n",
    "    for i in range(number_of_execution): # the middle loops iterates number_of_execution times\n",
    "        r = 0 \n",
    "        attempt = 0\n",
    "        while r != 3: # the while-loop iterates as long as r is not equal to 3\n",
    "            r = randrange(10) # randomly pick a number\n",
    "            attempt = attempt + 1 # increase the number of attempts by 1\n",
    "        # I am out of scope of while-loop\n",
    "        total_attempts = total_attempts + attempt # update the total number of attempts\n",
    "    # I am out of scope of for-loop\n",
    "    print(number_of_execution,\"->\",total_attempts/number_of_execution)\n",
    "\n",
    "# you can include 2 million to the list, but you should WAIT for a while to see the result\n",
    "# can your computer compete with exponential growth?\n",
    "# if you think \"yes\", please try 20 million, 200 million, and so on"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
